﻿
Imports System.Collections.Specialized

Namespace nvchelper
    Public Module nvchelper

        Public Delegate Sub traversalAction(ByVal name As String, ByVal value As String, ByVal para As Object)

        Public Sub traversal(ByVal dest As NameValueCollection, ByVal cb As traversalAction, _
                             Optional ByVal para As Object = Nothing)
            If debugassert(Not dest Is Nothing AndAlso Not cb Is Nothing, "dest or callback is nothing.") Then
                For i As Int32 = 0 To dest.Count() - 1
                    Try
                        Dim s() As String = Nothing
                        s = dest.GetValues(i)
                        If Not s Is Nothing AndAlso s.Length() > 0 Then
                            For j As Int32 = 0 To s.Length() - 1
                                cb(dest.GetKey(i), s(j), para)
                            Next
                        End If
                    Catch ex As Exception
                        If isdebugmode() Then
                            raiseError("cannot callback NameValueCollection " + dest.GetKey(i) + _
                                       ", value " + dest.Get(i) + ", ex " + ex.Message, errorHandle.errorType.system)
                        End If
                    End Try
                Next
            End If
        End Sub

        Private Sub copycb(ByVal name As String, ByVal value As String, ByVal para As Object)
            convertor.cast(Of NameValueCollection)(para).Set(name, value)
        End Sub

        Public Sub copy(ByVal dest As NameValueCollection, ByVal src As NameValueCollection)
            traversal(src, AddressOf copycb, dest)
        End Sub

        Public Function add(ByVal dest As NameValueCollection, ByVal name As String, ByVal value As String) As Boolean
            If debugassert(Not dest Is Nothing, "dest is nothing.") Then
                Try
                    dest.Add(name, value)
                    Return True
                Catch ex As Exception
                    raiseError("cannot add name value pair <" + name + "> to NameValueCollection, ex " + ex.Message, _
                               errorHandle.errorType.system)
                    Return False
                End Try
                Return True
            Else
                Return False
            End If
        End Function

        Public Function remove(ByVal dest As NameValueCollection, ByVal name As String) As Boolean
            If debugassert(Not dest Is Nothing, "dest is nothing.") Then
                Try
                    dest.Remove(name)
                    Return True
                Catch ex As Exception
                    raiseError("cannot remove name <" + name + "> from NameValueCollection, ex " + ex.Message, _
                               errorHandle.errorType.system)
                    Return False
                End Try
            Else
                Return False
            End If
        End Function

        Public Function modify(ByVal dest As NameValueCollection, _
                               ByVal name As String, ByVal value As String) As Boolean
            Return remove(dest, name) AndAlso add(dest, name, value)
        End Function
    End Module
End Namespace

Public Module nvcolhelper
    Public Sub copyNameValueCollection(ByVal dest As NameValueCollection, ByVal src As NameValueCollection)
        nvchelper.copy(dest, src)
    End Sub
End Module
